#include "urevisebookwidget.h"
#include "udispbookwidget.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
UReviseBookWidget::UReviseBookWidget(QWidget *parent)
	: QWidget(parent)
	, m_pISBN(new QLabel(this))
	, m_pTitle(new QLabel(this))
	, m_pISBNEdit(new QLineEdit(this))
	, m_pTitleEdit(new QLineEdit(this))
	, m_pQueryButton(new QPushButton(this))
	, m_pClearButton(new QPushButton(this))
	, m_pDispBookWidget(new UDispBookWidget(this))

	, m_pTitle1(new QLabel(this))
	, m_pAuthor(new QLabel(this))
	, m_pPress(new QLabel(this))
	, m_pIsbn(new QLabel(this))
	, m_pDate(new QLabel(this))
	, m_pImage(new QLabel(this))
	, m_pTitleEdit1(new QLineEdit(this))
	, m_pAuthorEdit(new QLineEdit(this))
	, m_pPressEdit(new QLineEdit(this))
	, m_pIsbnEdit(new QLineEdit(this))
	, m_pDateEdit(new QLineEdit(this))

	, m_pUpdateButton(new QPushButton(this))
	, m_pReviseButton(new QPushButton(this))
{
	setWindowTitle("修改信息");
	setAttribute(Qt::WA_StyledBackground, true);
	setStyleSheet(R"(UReviseBookWidget{background-color:#F5F5F5;border-radius:4px;border:1px solid #D9D8D7;border:none;})");
	resize(680, 700);
	m_pDispBookWidget->move(15, 90);
	initLabel();
	initLineEdit();
	initButton();
	initReviseControl();
	connect(m_pQueryButton, &QPushButton::released, this, &UReviseBookWidget::onQueryButtonReleased);
	connect(m_pClearButton, &QPushButton::released, this, &UReviseBookWidget::onClearButtonReleased);
	connect(m_pUpdateButton, &QPushButton::released, this, &UReviseBookWidget::onUpdateButtonReleased);
	connect(m_pReviseButton, &QPushButton::released, this, &UReviseBookWidget::onReviseButtonReleased);
}

UReviseBookWidget::~UReviseBookWidget()
{}

void UReviseBookWidget::onReviseButtonReleased()
{
	if (!m_pIsbnEdit->text().isEmpty() && !m_pTitleEdit1->text().isEmpty() && !m_pAuthorEdit->text().isEmpty() &&
		!m_pPressEdit->text().isEmpty() && !m_pDateEdit->text().isEmpty())
		UClientServer::Instance()->reviseBook(m_pTitleEdit1->text(), m_pAuthorEdit->text(), m_pPressEdit->text()
			, m_pIsbnEdit->text(), m_pDateEdit->text(), initialISBN);
}

void UReviseBookWidget::onUpdateButtonReleased()
{
	if (!m_pDispBookWidget->m_pBooksInfoList.empty())
	{
		for (UBooksInfo f : m_pDispBookWidget->m_pBooksInfoList) //后续可以执行批量删除处理
		{
			m_pTitleEdit1->setText(f.m_title);
			m_pAuthorEdit->setText(f.m_author);
			m_pIsbnEdit->setText(f.m_isbn);
			initialISBN = f.m_isbn;
			m_pPressEdit->setText(f.m_press);
			m_pDateEdit->setText(f.m_date);
		}
	}
}

void UReviseBookWidget::onQueryButtonReleased()
{
	m_pDispBookWidget->clearWidget();
	if (!m_pISBNEdit->text().isEmpty() || !m_pTitleEdit->text().isEmpty())
		UClientServer::Instance()->queryBook(m_pTitleEdit->text(), m_pISBNEdit->text(), "");
}

void UReviseBookWidget::onClearButtonReleased()
{
	m_pDispBookWidget->clearWidget();
	m_pISBNEdit->clear();
	m_pTitleEdit->clear();

	m_pTitleEdit1->clear();
	m_pAuthorEdit->clear();
	m_pPressEdit->clear();
	m_pIsbnEdit->clear();
	m_pDateEdit->clear();
}

void UReviseBookWidget::initButton()
{
	QString ButtonStyleSheet = R"(QPushButton{background-color:#808080;border:none;font:#000000;font-family:微软雅黑;font-size: 12px;}
									QPushButton:hover{background-color:#E9E9E9;})";
	m_pQueryButton->resize(60, 25);
	m_pQueryButton->move(430, 55);
	m_pQueryButton->setText("查询");
	m_pQueryButton->setStyleSheet(ButtonStyleSheet);

	m_pClearButton->resize(60, 25);
	m_pClearButton->move(500, 55);
	m_pClearButton->setText("清空");
	m_pClearButton->setStyleSheet(ButtonStyleSheet);

	m_pUpdateButton->resize(60,25);
	m_pUpdateButton->move(295, 630);
	m_pUpdateButton->setText("更新");
	m_pUpdateButton->setStyleSheet(ButtonStyleSheet);

	m_pReviseButton->resize(60, 25);
	m_pReviseButton->move(360, 630);
	m_pReviseButton->setText("确认");
	m_pReviseButton->setStyleSheet(ButtonStyleSheet);
}

void UReviseBookWidget::initLabel()
{
	QString LabelStyleSheet = R"(QLabel{border:none;font:#000000;font-family:微软雅黑;font-size:15px;})";
	m_pTitle->resize(60, 25);
	m_pTitle->move(15, 15);
	m_pTitle->setText("书名:");
	m_pTitle->setStyleSheet(LabelStyleSheet);

	m_pISBN->resize(60, 25);
	m_pISBN->move(350, 15);
	m_pISBN->setText("ISBN:");
	m_pISBN->setStyleSheet(LabelStyleSheet);
}

void UReviseBookWidget::initLineEdit()
{
	QString LineEditStyleSheet = R"(QLineEdit{background-color:#FFFFFF;border:none;font:#000000;font-family:微软雅黑;font-size:14px;})";
	m_pTitleEdit->resize(220, 25);
	m_pTitleEdit->move(75, 15);
	m_pTitleEdit->setStyleSheet(LineEditStyleSheet);

	m_pISBNEdit->resize(220, 25);
	m_pISBNEdit->move(410, 15);
	m_pISBNEdit->setStyleSheet(LineEditStyleSheet);
}

void UReviseBookWidget::initReviseControl()
{
	QString LabelStyleSheet = R"(QLabel{border:none;font:#000000;font-family:微软雅黑;font-size:15px;})";
	m_pTitle1->resize(60, 25);
	m_pTitle1->setText("书名");
	m_pTitle1->move(15, 520);
	m_pTitle1->setStyleSheet(LabelStyleSheet);

	m_pAuthor->resize(60, 25);
	m_pAuthor->setText("作者");
	m_pAuthor->move(15, 580);
	m_pAuthor->setStyleSheet(LabelStyleSheet);

	m_pPress->resize(60, 25);
	m_pPress->setText("出版社");
	m_pPress->move(15, 640);
	m_pPress->setStyleSheet(LabelStyleSheet);

	m_pIsbn->resize(60, 25);
	m_pIsbn->setText("ISBN");
	m_pIsbn->move(230, 520);
	m_pIsbn->setStyleSheet(LabelStyleSheet);

	m_pDate->resize(60, 25);
	m_pDate->setText("出版日期");
	m_pDate->move(230, 580);
	m_pDate->setStyleSheet(LabelStyleSheet);

	QString LineEditStyleSheet = R"(QLineEdit{background-color:#FFFFFF;border:none;font:#000000;font-family:微软雅黑;font-size:14px;})";
	m_pTitleEdit1->resize(150, 25);
	m_pTitleEdit1->move(75, 520);
	m_pTitleEdit1->setStyleSheet(LineEditStyleSheet);

	m_pAuthorEdit->resize(150, 25);
	m_pAuthorEdit->move(75, 580);
	m_pAuthorEdit->setStyleSheet(LineEditStyleSheet);

	m_pPressEdit->resize(150, 25);
	m_pPressEdit->move(75, 640);
	m_pPressEdit->setStyleSheet(LineEditStyleSheet);

	m_pIsbnEdit->resize(150, 25);
	m_pIsbnEdit->move(295, 520);
	m_pIsbnEdit->setStyleSheet(LineEditStyleSheet);

	m_pDateEdit->resize(150, 25);
	m_pDateEdit->move(295, 580);
	m_pDateEdit->setStyleSheet(LineEditStyleSheet);

}